var timer;


var get_message = function() {
    const url_ = `/apiArticle/message/`;
    let regexp = /.*csrftoken=([^;.]*).*$/;
    let xCSRF = document.cookie.match(regexp) === null ? null : document.cookie.match(regexp)[1];
    let regexp_token = /.*jwtToken=([^;.]*).*$/;
    let token = document.cookie.match(regexp_token) === null ? null : document.cookie.match(regexp_token)[1];
    fetch(url_, {
        method: 'GET',
        headers: {
            'token': token,
            'X-CSRFToken': xCSRF,
        },
    })
        .then(resp => resp.json())
        .then(json => {
            if (json.code !== 200) {
                
            } else {
                /*获取用户信息*/
                if (json.data.results.length !==0 && document.location.href.indexOf('personalPage') ===-1){
                    Vue.prototype.$notify.info({
                        title: '消息通知',
                        dangerouslyUseHTMLString: true,
                        duration: 30 * 1000,
                        message: `您有${json.data.results.length}条新消息未读 <a href="/pages/personalPage.html/" target="">点击前往</a>`
                      });
                }
                
            }
        })
}

var ping = function () {
    const url = `/apiUser/live/`;
    let regexp = /.*csrftoken=([^;.]*).*$/;
    let xCSRF = document.cookie.match(regexp) === null ? null : document.cookie.match(regexp)[1];
    let regexp_token = /.*jwtToken=([^;.]*).*$/;
    let token = document.cookie.match(regexp_token) === null ? null : document.cookie.match(regexp_token)[1];
    if (token === null) {
        Vue.prototype.$message({
            message: `登录未验证 有些功能可能会受限哦`,
            type: 'warning'
        });
        return 
    }
    fetch(url, {
        method: 'GET',
        headers: {
            'token': token,
            'X-CSRFToken': xCSRF,
        },
    })
        .then(resp => resp.json())
        .then(json => {
            if (json.code !== 200) {
                Vue.prototype.$message({
                    message: `登录未验证 有些功能可能会受限哦`,
                    type: 'warning'
                });
                localStorage.removeItem('token');
                localStorage.removeItem('nick_name');
                window.clearCookie("jwtToken");
                clearInterval(timer);
            } else {
                /*获取用户信息*/
                get_message()
            }
        })
}


ping();

timer = setInterval(
    ping, 1000 * 30
)

